package com.hanxu.mallware.service.impl;

import org.springframework.stereotype.Service;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanxu.common.utils.PageUtils;
import com.hanxu.common.utils.Query;

import com.hanxu.mallware.dao.PurchaseDetailDao;
import com.hanxu.mallware.entity.PurchaseDetailEntity;
import com.hanxu.mallware.service.PurchaseDetailService;
import org.springframework.util.StringUtils;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.hasText(key)) {
            String tmpKey = URLDecoder.decode(key, StandardCharsets.UTF_8);
            wrapper.and((queryWrapper) -> {
                queryWrapper.eq("purchase_id", tmpKey).or().eq("sku_id", tmpKey);
            });
        }

        String status = (String) params.get("status");
        if (StringUtils.hasText(key)) {
            wrapper.and((queryWrapper) -> {
                queryWrapper.eq("status", status);
            });
        }

        String wareId = (String) params.get("wareId");
        if (StringUtils.hasText(key)) {
            wrapper.and((queryWrapper) -> {
                queryWrapper.eq("ware_id", wareId);
            });
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

}